package cn.mavor.day0910.Student;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class FileUtils {
    public static final String DATA_FILE = "C:\\Users\\Marvo\\IdeaProjects\\class\\data.txt";
    public static final String STUDS_FILE = "studs.txt";
    public static final String TEMP_FILE = "temp.txt";

    public static List<String> readAllLines(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        if (!Files.exists(path)) {
            throw new FileNotFoundException("文件 " + fileName + " 不存在。");
        }
        return Files.readAllLines(path);
    }

    public static void writeAllLines(String fileName, List<String> lines) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine();
            }
        }
    }

    public static void deleteFile(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        if (Files.exists(path)) {
            Files.delete(path);
        } else {
            throw new FileNotFoundException("文件 " + fileName + " 不存在。");
        }
    }

    public static void moveFile(String source, String target) throws IOException {
        Path sourcePath = Paths.get(source);
        Path targetPath = Paths.get(target);
        if (!Files.exists(sourcePath)) {
            throw new FileNotFoundException("源文件 " + source + " 不存在。");
        }
        Files.move(sourcePath, targetPath);
    }

    // 添加公共方法来获取文件名
    public static String getDataFile() {
        return DATA_FILE;
    }

    public static String getStudsFile() {
        return STUDS_FILE;
    }

    public static String getTempFile() {
        return TEMP_FILE;
    }
}